package com.itheima.controller;

import com.alibaba.fastjson.JSONObject;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.resource.MessageConstant;
import com.itheima.service.CheckGruopService;
import com.itheima.service.CheckItemService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {

    @Reference
    private CheckGruopService checkGruopService;

    /**
     * 添加检查组
     * @param map
     * @return
     */
    @RequestMapping("/addgroup.do")
    public Result addGroup(@RequestBody Map<String,Object> map){//使用map接收参数
        try {
            //使用fastjson封装数据，自动转换到实体类对象
            CheckGroup checkGroup=
                    JSONObject.parseObject(map.get("group").toString(),CheckGroup.class);


            Integer[] checkitemIds=JSONObject.parseObject(map.get("ids").toString(),Integer[].class);

            System.out.println(checkGroup);
            System.out.println(checkitemIds);

            checkGruopService.addGroup(checkGroup,checkitemIds);
            return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_CHECKGROUP_FAIL);
        }

    }

    /**
     * 检查组条件查询
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage.do")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {

        try {
            Map map = checkGruopService.findPage(queryPageBean);
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }


    }

    /**
     * 根据检查组的id获取对应的检查项的id
     * @param id
     * @return
     */
    @RequestMapping("/findCheckItemIdsByCheckGroupId.do")
    public Result findCheckItemIdsByCheckGroupId(@RequestParam("id") Integer id){
        try {
            Integer[] checkitemIds=checkGruopService.findCheckItemIdsByCheckGroupId(id);
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkitemIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_SUCCESS);
        }

    }

    /**
     * 编辑检查组
     * @return
     */
    @RequestMapping("/editgroup.do")
    public Result editgroup(@RequestBody Map<String,Object> map){
        try {
            //使用fastjson封装数据，自动转换到实体类对象
            CheckGroup checkGroup=
                    JSONObject.parseObject(map.get("group").toString(),CheckGroup.class);


            Integer[] checkitemIds=JSONObject.parseObject(map.get("ids").toString(),Integer[].class);

            System.out.println(checkGroup);
            System.out.println(checkitemIds);

            checkGruopService.editGroup(checkGroup,checkitemIds);
            return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_CHECKGROUP_FAIL);
        }
    }


    /**
     * 删除检查组
     * @param id
     * @return
     */
    @RequestMapping("/deleteCheckGroupById.do")
    public Result deleteCheckGroupById(@RequestParam("id") Integer id){
        try {
            checkGruopService.deleteCheckGroupById(id);
            return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }

    }

    /**
     * 查询所有检查组
     * @return
     */
    @RequestMapping("/findAll.do")
    public Result findAll(){
        try {
            List<CheckGroup> checkGroupList = checkGruopService.findAll();
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkGroupList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }
}
